//
//  ChannelManager.m
//  Runner
//
//  Created by skp on 2023/2/27.
//

#import "ChannelManager.h"

#define MethodChannel     @"MethodChannel"


@interface ChannelManager ()
@property (nonatomic, strong) FlutterMethodChannel *channel;
@end

@implementation ChannelManager

+ (ChannelManager *)shared  {
    static ChannelManager *channel;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        channel = [[ChannelManager alloc] init];
    });
    return channel;
}

- (void)startMonitorFlutter {
    __weak typeof(self) weakSelf = self;
    [self.channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        NSString *method = call.method;
        if ([method isEqualToString:@"openAppStore"]) {
            NSString *path = @"itms-apps://itunes.apple.com";
            NSURL *url = [NSURL URLWithString:path];
            if ([[UIApplication sharedApplication] canOpenURL:url]) {
              [[UIApplication sharedApplication] openURL:url];
            }
        }
    }];
}

- (void)callFlutter:(NSString *)method params:(NSDictionary *)params {
    
}


- (FlutterMethodChannel *)channel {
    if (!_channel) {
        _channel = [FlutterMethodChannel methodChannelWithName:MethodChannel binaryMessenger:self.flutterCtr.binaryMessenger];
    }
    return _channel;
}

@end
